package com.hsmpay.common.util.system;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class Constant {

    private static Map<String, String> map;
    public static String rootPath ;//根目录
    static {
        ParseConstant();
        getRootPath();
    }

    public static String getAttribute(String key) {
        return map.get(key) != null ? map.get(key) : "";
    }

    @SuppressWarnings("unchecked")
    private static void ParseConstant() {
        String key = "";
        String value = "";
        map = new HashMap<String, String>();
        SAXReader saxReader = new SAXReader();
        try {
            Document document = saxReader.read(Constant.class.getClassLoader().getResourceAsStream("constant.xml"));
            Element root = document.getRootElement();
            // 遍历根结点root的所有子节点element
            for (Iterator iter = root.elementIterator(); iter.hasNext();) {
                Element element = (Element) iter.next();
                // 遍历结点element的子节点（key，value），并进行处�?
                for (Iterator iterInner = element.elementIterator(); iterInner
                        .hasNext();) {
                    Element elementInner = (Element) iterInner.next();
                    if (elementInner.getName().equals("key")) {
                        key = elementInner.getText();
                    }
                    if (elementInner.getName().equals("value")) {
                        value = elementInner.getText();
                    }
                    if ((!"".equals(key)) && (!"".equals(value)) && (key != null) && (value != null)) {
                        map.put(key, value);
                        key = null;
                        value = null;
                    }
                }
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    @SuppressWarnings("unchecked")
    public static HashMap<String, String> getDataHashMap(String path) {
        HashMap<String, String> returnValue = new HashMap<String, String>();
        SAXReader saxReader = new SAXReader();
        String key = "";
        String value = "";
        try {
            Document document = saxReader.read(Constant.class.getClassLoader().getResourceAsStream(path));
            Element root = document.getRootElement();
            // 遍历根结点root的所有子节点element
            for (Iterator iter = root.elementIterator(); iter.hasNext();) {
                Element element = (Element) iter.next();
                // 遍历结点element的子节点（key，value），并进行处�?
                for (Iterator iterInner = element.elementIterator(); iterInner.hasNext();) {
                    Element elementInner = (Element) iterInner.next();
                    if (elementInner.getName().equals("key")) {
                        key = elementInner.getText();
                    }
                    if (elementInner.getName().equals("value")) {
                        value = elementInner.getText();
                    }
                    if ((!"".equals(key)) && (!"".equals(value)) && (key != null) && (value != null)) {
                        System.out.println("key" + key+":"+value);
                        returnValue.put(key, value);
                        key = null;
                        value = null;
                    }
                }
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return returnValue;

    }

    public static String getRootPath() {
        String classPath = Constant.class.getClassLoader().getResource("/").getPath();
        if("\\".equals(File.separator)){//windows下
            rootPath  = classPath.substring(1,classPath.indexOf("/WEB-INF/classes"));
            rootPath = rootPath.replace("/", "\\");
        }else if("/".equals(File.separator)){//linux unix
            rootPath  = classPath.substring(0,classPath.indexOf("/WEB-INF/classes"));
            rootPath = rootPath.replace("\\", "/");
        }
        return rootPath;
    }


    public static void main(String[] args) {
        String path = "com/hsmpay/back/util/system/constant.xml";
        HashMap<String, String> map = getDataHashMap(path);
        System.out.println("map:" + map.entrySet());
        System.out.println("MEMCACHE_EXPIRE:" + getAttribute("BATCH_CLEARING_UPLOAD_XLS"));
    }
}
